package vn.wallet.app.helper;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Validator {

	public static final String	USERNAME_PATTERN	= "^[a-z0-9_-]{3,15}$";
	public static final String	PASSWORD_PATTERN	= "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";
	public static final String	EMAIL_PATTERN		= "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	public static final String	IMAGE_PATTERN		= "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";
	public static final String	IPADDRESS_PATTERN	= "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
															+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
															+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
															+ "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
	public static final String	FLOAT_NUMBER		= "^-?(?:[0-9]+|[0-9]*\\.[0-9]+)$";

	public static boolean isEmailAddress(String email) {
		Pattern pattern = Pattern.compile(EMAIL_PATTERN);
		Matcher matcher = pattern.matcher(email);

		return matcher.matches();
	}

	/**
	 * Validate username with regular expression
	 * 
	 * @param username
	 *            for validation
	 * @return true valid username, false invalid username
	 * @return
	 */
	public static boolean isValidUserName(final String username) {
		Pattern pattern = Pattern.compile(USERNAME_PATTERN);
		Matcher matcher = pattern.matcher(username);

		return matcher.matches();
	}

	/**
	 * Validate password with regular expression
	 * 
	 * @param password
	 *            for validation
	 * @return true valid password, false invalid password
	 * @return
	 */
	public static boolean isValidPassword(final String password) {
		Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
		Matcher matcher = pattern.matcher(password);

		return matcher.matches();
	}

	/**
	 * Validate image with regular expression
	 * 
	 * @param image
	 *            for validation
	 * @return true valid image, false invalid image
	 */
	public static boolean isValidImageFile(final String image) {
		Pattern pattern = Pattern.compile(IMAGE_PATTERN);
		Matcher matcher = pattern.matcher(image);

		return matcher.matches();
	}

	public static boolean isValidIpAddress(final String ip) {
		Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
		Matcher matcher = pattern.matcher(ip);

		return matcher.matches();
	}

	@SuppressWarnings("rawtypes")
	public static boolean isEmptyList(final List list) {
		if (list == null || list.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public static boolean isAmount(String amount) {
		Pattern pattern = Pattern.compile(FLOAT_NUMBER);
		Matcher matcher = pattern.matcher(amount);
		return matcher.matches();
	}

}
